﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using CM.DL.DALC.NC03;
using DECOR.BL.BE;

namespace DECOR.DL.DALC.NC03
{
    public class PersonaDALC : IPersonaDALC
    {
        private string _connId = string.Empty;

        public static PersonaDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(PersonaDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new PersonaDALC(pConnId);
                    }
                }
            }
            return _instance;
        }

        private PersonaDALC(string pConnId) { this._connId = pConnId; }
        private PersonaDALC() { }
        private static volatile PersonaDALC _instance = null;

        #region IPersonaDALC Members

        public List<DECOR.BL.BE.PersonaBE> listarVendedores()
        {
            throw new NotImplementedException();
        }

        public List<DECOR.BL.BE.PersonaBE> listarProfesionales(string pNombre)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            List<PersonaBE> listaResult = new List<PersonaBE>();
            DestConfig dc = new DestConfig();
            try
            {
                /*try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                 */
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_PROFESIONALES");
                function.SetValue("IM_NOMBRE", pNombre);
                function.Invoke(dest);

                IRfcTable T_PA0001 = function.GetTable("T_PA0001");

                foreach (IRfcStructure item in T_PA0001)
                {
                    PersonaBE obj = new PersonaBE();
                    obj.CodPersona = item["PERNR"].GetString();
                    obj.Descripcion = item["ENAME"].GetString();

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        public DECOR.BL.BE.PersonaBE leer(string pCodPersona)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            PersonaBE obj = null;
            DestConfig dc = new DestConfig();
            try
            {
                /*try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                */
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LEE_PERSONA");
                function.SetValue("IM_PERNR", pCodPersona);
                function.SetValue("IM_DNI", pCodPersona);
                
                function.Invoke(dest);

                IRfcTable PA0002 = function.GetTable("T_PA0002");
                                
                foreach (IRfcStructure item in PA0002)
                {
                    obj = new PersonaBE();
                    
                    obj.CodPersona = item["PERNR"].GetString();
                    obj.Nombre = item["VORNA"].GetString();
                    obj.Apellido = item["NACHN"].GetString();
                    obj.ApeSoltero = item["NAME2"].GetString();
                    obj.Iniciales = item["INITS"].GetString();
                    obj.Descripcion = (item["VORNA"].GetString() + " " + item["NACHN"].GetString() + " " + item["NAME2"].GetString()).Trim();
                    obj.Dni = item["RUFNM"].GetString();
                    obj.Telefono = item["TITEL"].GetString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return obj;
        }

        public void getInfoUsuario(string pCodPersona, out string pCodOfVtas, out string pCodGrupoVend)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
